package com.jerry.springcloud.service.impl;

import com.jerry.springcloud.service.IMessageProvider;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;

import javax.annotation.Resource;
import java.util.UUID;

@EnableBinding(Source.class)    // 定义消息的发送端
public class MessageProviderImpl implements IMessageProvider {

    @Resource
    private MessageChannel output;     //消息发送管道

    @Override
    public String send() {
        String serial = UUID.randomUUID().toString();
        Message message = MessageBuilder.withPayload(serial).build();
        String result;
        if (output.send(message)) {
            result = "发送成功 ***** serial=" + serial;
        } else {
            result = "发送失败 *************";
        }
        System.out.println(result);
        return result;
    }
}
